package br.panthro.utils.config
{
	import br.panthro.utils.xml.XMLEvent;
	import br.panthro.utils.xml.XMLLoader;
	
	import flash.display.Sprite;
	
	
	public class Config extends Sprite
	{
		
		private var _configFile:String;
		private var _xmlLoader:XMLLoader;
		private var _configData:XML;
		private var _pages:Array;
		private var _siteStructure:Array;
		private var _fontsList:Array;
		private var _gateways:Object;
		private var _background:Object;
		private var _parameters:Object;
		
		public function set configFile(configFile:String):void
		{
			if(!_configFile)
			{
				_configFile = configFile;
				loadConfig();
			} else {
				trace("You can't have more than one config file");
			}
		}
		
		private function loadConfig():void
		{
			_xmlLoader = new XMLLoader();
			_xmlLoader.load(_configFile);
			_xmlLoader.addEventListener(XMLEvent.LOAD_COMPLETE, onComplete);
			_xmlLoader.addEventListener(XMLEvent.LOAD_ERROR, onError);
		}
		
		private function onComplete(event:XMLEvent):void
		{
			_configData = _xmlLoader.content;
			if(!_configData)
			{
				trace("xml not loaded");
				dispatchEvent(new ConfigEvent(ConfigEvent.CONFIG_ERROR));	
			} else {
				if(!_configData.parameters)
				{
					trace("Malformed XML: No parameters found.");
					dispatchEvent(new ConfigEvent(ConfigEvent.CONFIG_ERROR));
				} else {
					getParameters();
				}
			}
		}
		private function onError(event:XMLEvent):void
		{
			dispatchEvent(new ConfigEvent(ConfigEvent.CONFIG_ERROR));
		}
		
		private function getParameters():void
		{
			_parameters = {};
			for each(var _node:XML in _configData.parameters.children())
			{
				if(_node.children().length() > 1)
				{
					if(_node.localName() == "background")
					{
						var backgroundData:XML = _node;
						var backgroundScale:String;
						
						if(backgroundData.elements("scale").length() > 0)
						{
							backgroundScale = backgroundData.scale.type;
						} else 
						{
							backgroundScale = "noScale";
						}
						
						if(backgroundData.type == "video" || backgroundData.type == "image")
						{
							_background = {type: backgroundData.type, 
								contentPath: backgroundData.contentPath, 
								scale: backgroundScale, 
								alignment: backgroundData.alignment};
						} else if(backgroundData.type == "gradient") {
							_background = {type: backgroundData.type, 
								gradientType: backgroundData.gradientType,
								baseColor: backgroundData.baseColor, 
								secondaryColor: backgroundData.secondaryColor, 
								focalPoint:backgroundData.focalPoint,
								scale:"FIT", alignment: "CENTER"};
						} else if(backgroundData.type == "solid")
						{
							_background = {type: backgroundData.type, 
								color: backgroundData.color, 
								scale:"FIT", alignment: "CENTER"};
						}
						_parameters.background = _background;
					} else if(_node.localName() == "gateway")
					{
						_gateways = {local: _node.child(0),
								 	 main:_node.child(1)};
						
						_parameters.gateways = _gateways;
					}
				} else {
					var nodeVal:String = _node;
					var basepath:RegExp = /%\(basepath\)s/;
					var locale:RegExp = /%\(locale\)s/;
					var locales:RegExp = /%\(locales\)s/;
					
					if((basepath.test(nodeVal)))
					{	
						nodeVal = nodeVal.replace(basepath, _parameters.basepath);
					}
					if((locale.test(nodeVal)))
					{	
						nodeVal = nodeVal.replace(locale, _parameters.locale);
					}
					if((locales.test(nodeVal)))
					{	
						nodeVal = nodeVal.replace(locales, _parameters.locales);
					}
					
					
					
					_parameters[_node.localName()] = nodeVal;
				}
			}
			if(!_configData.site)
			{
				
				dispatchEvent(new ConfigEvent(ConfigEvent.CONFIG_ERROR));
			} else 
			{
				getPages();
			}
		}
		
		private function getPages():void
		{
			var pageObject:Object;
			_pages = [];
		
			for each(var pageNode:XML in _configData.site.children())
			{
				pageObject = {};
				for each(var confNode:XML in pageNode.children())
				{
					
					if(confNode.children().length() == 1)
					{
						var nodeVal:String = confNode;
						var title:RegExp = /%\(title\)s/;
						
						if((title.test(nodeVal)))
						{	
							nodeVal = nodeVal.replace(title, _parameters.title);
						}
						pageObject[confNode.localName()] = nodeVal; 
					} else {
						pageObject.dependencies = {};
						for each(var depNode:XML in confNode.children())
						{
							var depVal:String = depNode;
							var commonpath:RegExp = /%\(commonpath\)s/;
							
							if((commonpath.test(depVal)))
							{	
								depVal = depVal.replace(commonpath, _parameters.commonpath);
							}
							
							pageObject.dependencies[depNode.localName()] = depVal;
							
						}
						
					}
				}
				_pages.push(pageObject);
			}
			
			_parameters.pages = _pages;
			dispatchEvent(new ConfigEvent(ConfigEvent.CONFIG_LOADED));
		}
		public function get parameters():Object
		{
			return _parameters;
		}	
	}
}